package com.education.util;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * DateUtil.java基类<br>
 * 
 * @describe：
 * @author：zhangshijie
 * @since：Mar 25, 2010 11:05:37 AM
 */
public class DateUtil {

	private static final String DATE_YEAR = "yyyy";
	private static final String DATE_MONTH = "yyyy-MM";
	private static final String DATE_DAY = "yyyy-MM-dd";
	private static final String DATE_HOUR = "yyyy-MM-dd HH";
	private static final String DATE_MINUTES = "yyyy-MM-dd HH:mm";
	private static final String DATE_SENCOND = "yyyy-MM-dd HH:mm:ss";
	private static final String DATE_MILLISECOND = "yyyy-MM-dd HH:mm:ss.SSS";

	public static boolean isDate(Date date) {
		long time = date.getTime();
		return ((time + 28800000L) % 86400000L == -7784060008416673792L);
	}

	public static Date toDate(Date date) {
		long time = date.getTime();
		time -= (time + 28800000L) % 86400000L;
		return new Date(time);
	}

	/**
	 * @Describe：返回N（delay）天以后的日期
	 * @author：张仕杰/zhangshijie
	 * @Date：May 14, 2010
	 */
	public static String getDateDelay(Date startDate, int delay) {
		return getDateDelay(startDate, null, delay);
	}

	/**
	 * @Describe：返回N（delay）天以后的日期
	 * @author：张仕杰/zhangshijie
	 * @Date：May 14, 2010
	 */
	public static String getDateDelay(Date startDate, String format, int delay) {
		if(DataUtil.isEmpty(format)) {
			format = DATE_SENCOND;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		long myTime = startDate.getTime() + delay * 24 * 60 * 60 * 1000;
		return sdf.format(new Date(myTime));
	}

	/**
	 * @Describe：返回N（delay）天以后的日期
	 * @author：张仕杰/zhangshijie
	 * @Date：May 14, 2010
	 */
	public static Date getDateDelay(String date, int delay) throws ParseException {
		return getDateDelay(date, null, delay);
	}

	/**
	 * @Describe：返回N（delay）天以后的日期
	 * @author：张仕杰/zhangshijie
	 * @Date：May 14, 2010
	 */
	public static Date getDateDelay(String date, String format, int delay) throws ParseException {
		if(DataUtil.isEmpty(format)) {
			format = DATE_SENCOND;
		}

		Date startDate = getDateFromString(date, format);
		long myTime = startDate.getTime() + delay * 24 * 60 * 60 * 1000;
		return new Date(myTime);
	}
	
	/**
	 * @Describe：返回N（delay）天以后的日期和时间
	 * @author：张仕杰/zhangshijie
	 * @Date：May 14, 2010
	 */
	public static String getStrDateTimeDelay(Date startDate, int delay) {
		SimpleDateFormat format = new SimpleDateFormat(DATE_MILLISECOND);
		long myTime = startDate.getTime() + delay * 24 * 60 * 60 * 1000;
		return format.format(new Date(myTime));
	}
	
	public static int getCurrentTime(String type) {
		if(DataUtil.isEmpty(type)) {
			throw new RuntimeException("please input you want convert type paras...");
		}
		
		Calendar c = Calendar.getInstance();
		
		int result = 0;		
		if("yyyy".equals(type)) {
			result = c.get(Calendar.YEAR);
		}
		
		if("MM".equals(type)) {
			result = c.get(Calendar.MONTH) + 1;
		}
		
		if("dd".equals(type)) {
			result = c.get(Calendar.DATE);
		}
		
		if("w".equals(type)) {
			result = getWeekDay(c);
		}
		
		if("HH".equals(type)) {
			result = c.get(Calendar.HOUR_OF_DAY);
		}
		
		if("mm".equals(type)) {
			result = c.get(Calendar.MINUTE);
		}
		
		if("ss".equals(type)) {
			result = c.get(Calendar.SECOND);
		}
		
		if("sss".equals(type)) {
			result = c.get(Calendar.MILLISECOND);
		}
		return result;		
	}	

	private static int getWeekDay(Calendar c) {
		if (c == null) {
			throw new RuntimeException("Calendar is NULL...");
		}

		int result = 0;
		if (Calendar.MONDAY == c.get(Calendar.DAY_OF_WEEK)) {
			result = 1;
		}
		if (Calendar.TUESDAY == c.get(Calendar.DAY_OF_WEEK)) {
			result = 2;
		}
		if (Calendar.WEDNESDAY == c.get(Calendar.DAY_OF_WEEK)) {
			result = 3;
		}
		if (Calendar.THURSDAY == c.get(Calendar.DAY_OF_WEEK)) {
			result = 4;
		}
		if (Calendar.FRIDAY == c.get(Calendar.DAY_OF_WEEK)) {
			result = 5;
		}
		if (Calendar.SATURDAY == c.get(Calendar.DAY_OF_WEEK)) {
			result = 6;
		}
		if (Calendar.SUNDAY == c.get(Calendar.DAY_OF_WEEK)) {
			result = 7;
		}
		
		return result;
	}

	public static int getDayByTwoDate(Date startDate, Date endDate) {
		int day = 0;
		long millTime = endDate.getTime() - startDate.getTime();
		day = (int) (millTime / 86400000L);
		return day;
	}
	
	public static Date getDateFromDate(Date date,String pattern) throws ParseException {
		if(date == null)
			return date;
		String dateStr = getStringFromDate(date, pattern);
		return getDateFromString(dateStr, pattern);
	}
	
	/**
	 * 按照指定的格式把String类型的字符串转换为Date类型
	 * @param dateStr
	 * @param pattern
	 * @return
	 * @throws java.text.ParseException
	 */
	public static Date getDateFromString(String dateStr,String pattern) throws ParseException {
		if(DataUtil.isEmpty(dateStr)) {
			return null;
		}
		if(DataUtil.isEmpty(pattern))
			pattern = DATE_SENCOND;
		/*SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		return formatter.parse(dateStr);*/

		DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(DATE_SENCOND);
		DateTime dateTime = dateTimeFormatter.parseDateTime(dateStr);
		return dateTime.toDate();
	}

	/**
	 * 按照指定的格式把String类型的字符串转换为Date类型
	 * @param dateStr
	 * @return
	 * @throws java.text.ParseException
	 */
	public static Date getDateFromString(String dateStr) throws ParseException {
		if(DataUtil.isEmpty(dateStr)) {
			return null;
		}

		return getDateFromString(dateStr, null);
	}

	/**
	 * 将指定的日期按照指定的Type转换为String类型
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String getStringFromDate(Date date,String pattern) {
		if(date == null)
			date = new Date();
		if(DataUtil.isEmpty(pattern))
			pattern = DATE_SENCOND;
		//return new SimpleDateFormat(pattern).format(date);

		DateTime time = new DateTime(date);
		return time.toString(pattern);
	}

	/**
	 * 将指定的日期按照指定的Type转换为String类型
	 * @param date
	 * @return
	 */
	public static String getStringFromDate(Date date) {
		return getStringFromDate(date, null);
	}
	
	/**
	 * 将毫秒数换算成x天x时x分x秒x毫秒
	 * 
	 * @param obj
	 *            毫秒
	 * @return String 441452888L=[05天02小时37分32秒]
	 */
	public static String getStringFromMillisecond(Object obj) {
		long ms = 0;
		if (obj == null)
			return "";

		if (obj instanceof String || obj instanceof Long)
			ms = Long.parseLong(obj.toString());

		int ss = 1000;
		int mi = ss * 60;
		int hh = mi * 60;
		int dd = hh * 24;

		long day = ms / dd;
		long hour = (ms - day * dd) / hh;
		long minute = (ms - day * dd - hour * hh) / mi;
		long second = (ms - day * dd - hour * hh - minute * mi) / ss;

		String str = "";
		String[] array = { "天", "时", "分", "秒", "毫秒" };
		if (day != 0)
			str += day + array[0];
		if (hour != 0)
			str += hour + array[1];
		if (minute != 0)
			str += minute + array[2];
		if (second != 0)
			str += second + array[3];

		// String strDay = day < 10 ? "0" + day : "" + day;
		// String strHour = hour < 10 ? "0" + hour : "" + hour;
		// String strMinute = minute < 10 ? "0" + minute : "" + minute;
		// String strSecond = second < 10 ? "0" + second : "" + second;
		// String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" +
		// milliSecond;
		// strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" +
		// strMilliSecond;
		// String str = strDay + " " + strHour + ":" + strMinute + ":" +
		// strSecond+ " " + strMilliSecond;
		// if(milliSecond!=0) str += milliSecond + array[4];
		return str;
	}
	
	public static void main(String[] args) {
		try {
			//System.out.println(getDateDelay("2015-01-21", DATE_DAY, 1));
			System.out.println(getStringFromDate(new Date()));
			System.out.println(getDateFromString("2015-01-21 00:00:00"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		//System.out.println(getDateFromDate(new Date(), "yyyy"));
//		System.out.println(getCurrentTime("yyyy") + "-" +  getCurrentTime("MM")
//				+ "-" + getCurrentTime("dd") + " " + getCurrentTime("HH") + ":" + getCurrentTime("mm")
//				+ ":" + getCurrentTime("ss") + ":" + getCurrentTime("sss") + " " + getCurrentTime("w"));
	}

}